package com.karry.yl.web.baseinfo;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.karry.commons.Constants;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.yl.domain.baseinfo.Certificate;
import com.karry.yl.domain.baseinfo.Organization;
import com.karry.yl.domain.system.*;
import com.karry.yl.service.baseinfo.CertificateService;
import com.karry.yl.service.baseinfo.OrganizationService;
import com.karry.yl.service.system.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/organization")
public class OrganizationController extends SuperController {

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private AreaService areaService;
    @Autowired
    private TownService townService;
    @Autowired
    private VillageService villageService;
    @Autowired
    private CertificateService certificateService;

    @RequestMapping
    public String ui(HttpSession session,ModelMap map){
        Organization org = organizationService.selectById(currentUser.getOrgId());
        session.setAttribute(Constants.SK_JG,org);
        List<Province> provinces = provinceService.selectList(null);
        map.put("provinceList",provinces);
        if(!StringUtils.isEmpty(org.getProvinceCode()) && !StringUtils.isEmpty(org.getCityCode()) && !StringUtils.isEmpty(org.getAreaCode()) && !StringUtils.isEmpty(org.getTownCode())){
            List<City> cityList = cityService.selectList(new EntityWrapper<City>().like("id", org.getProvinceCode().substring(0, 2),SqlLike.RIGHT));
            List<Area> areaList = areaService.selectList(new EntityWrapper<Area>().like("id",  org.getCityCode().substring(0, 4),SqlLike.RIGHT));
            List<Town> townList = townService.selectList(new EntityWrapper<Town>().like("id",  org.getAreaCode().substring(0, 6),SqlLike.RIGHT));
            List<Village> villageList = villageService.selectList(new EntityWrapper<Village>().like("id",  org.getTownCode().substring(0, 9),SqlLike.RIGHT));
            map.put("cityList",cityList);
            map.put("areaList",areaList);
            map.put("townList",townList);
            map.put("villageList",villageList);
        }
        List<Certificate> fzrs = certificateService.selectList(new EntityWrapper<Certificate>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("type",8));
        map.put("fzrs", fzrs);
        List<Certificate> jgzps = certificateService.selectList(new EntityWrapper<Certificate>().eq("orgId", currentUser.getOrgId()).eq("deleted", false).eq("type",9));
        map.put("jgzps", jgzps);
        return "baseinfo/jgxx";
    }

    @RequestMapping("/save")
    public String save(Organization organization,HttpSession session){
        organization.setId(currentUser.getOrgId());
        organizationService.updateById(organization);
        session.setAttribute(Constants.SK_JG,organization);
        return "redirect:/organization";
    }

    @RequestMapping("/ui/bindPrimary")
    public String bindPrimaryUI(ModelMap map){
        return "bindprimary";
    }

    @RequestMapping("/bindPrimary")
    @ResponseBody
    public JsonResponse bindPrimary(String primaryName,HttpSession session){
        Organization primaryOrg = organizationService.selectOne(new EntityWrapper<Organization>().eq("deleted",0).eq("name",primaryName));
        if(null != primaryOrg){
            Organization org = organizationService.selectById(currentUser.getOrgId());
            org.setPrimaryId(primaryOrg.getId());
            org.setPrimaryName(primaryOrg.getName());
            primaryOrg.setPrimary(true);
            organizationService.bindPrimary(primaryOrg,org);
            session.setAttribute(Constants.SK_JG,org);
            return JsonResponse.success("关联成功");
        }else {
            return JsonResponse.fail("要关联的主机构不存在,请核对名称是否正确");
        }
    }
    @RequestMapping("/delzshu")
    public String delete(Long id){
        certificateService.delete(new EntityWrapper<Certificate>().eq("orgId",currentUser.getOrgId()).eq("id",id));
        return "redirect:/organization";
    }
}
